﻿using System;
using System.Collections.Generic;
using System.Data;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace CSharp.UserControl
{
    public partial class ListUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void BindData(DataTable dt)
        {
            if (!DataTableEx.DataTable_OK(dt))
                return;
            List<SEOEO> lstTemp = new List<SEOEO>();
            lstTemp = (List<SEOEO>)ConvertEx.ConvertDataTableToListObject<SEOEO>(dt, lstTemp.GetType());
            Int32 iRowNum = 1;
            if (lstTemp != null && lstTemp.Count > 0)
            {
                KeyValueEO objTemp;
                Int32 iIndexTemp = 0;
                foreach (SEOEO item in lstTemp)
                {
                    iIndexTemp++;
                    objTemp = getListNodeButton(iIndexTemp, item);
                    item.iRowNum = iRowNum++;
                    item.sListNodeButton = objTemp.K;
                    item.sListNodeImage = objTemp.V;
                    //lstTemp.Add(new ShowListEO(Convert.ToInt64(dt.Rows[i]["PK_iDID"]), i, Convert.ToString(dt.Rows[i]["sTitle"]), Convert.ToString(dt.Rows[i]["sDescription"]), Convert.ToString(dt.Rows[i]["sUrlRewrite"]), Convert.ToString(dt.Rows[i]["sLinkImage"]), getListNodeButton(dt.Rows[i]["PK_iDID"], dt.Rows[i]["sLinkImage"]), getListNodeImage(dt.Rows[i]["sTitle"], dt.Rows[i]["sLinkImage"]), Convert.ToDateTime(dt.Rows[i]["tDateTimeCreate"])));
                }
            }

            //lstTemp.Add(new ShowListEO(123, 2, "Tiêu đề XYZ", "mô tả thông tin chi tiết về XYZ", "http://abc.xyz", "", "<li data-target=\"#carousel-1\" data-slide-to=\"0\" class=\"active\"></li>", "<div class=\"item active\"><img src=\"http://lorempixel.com/150/150?rand=1\" alt=\"Image\"></div>"));
            rptList.DataSource = lstTemp;
            rptList.DataBind();
        }

        protected KeyValueEO getListNodeButton(Int32 iIndexTemp, SEOEO objInput)
        {
            KeyValueEO objRet = new KeyValueEO();
            if (!String.IsNullOrEmpty(objInput.sListIDImage))
            {
                String[] arrTemp = objInput.sListIDImage.Split(',');


                //List<ResourceEO> lstTemp = new List<ResourceEO>();
                //lstTemp = (List<ResourceEO>)ConvertEx.ConvertDataTableToListObject<ResourceEO>((new ResourceDAO()).SelectBy_ListID(objInput.sListIDImage), lstTemp.GetType());
                //if (CommonsCheck.ListObject_OK<ResourceEO>(lstTemp))
                //{
                for (int i = 0; i < arrTemp.Length; i++)
                {
                    if (i == 0)
                    {
                        objRet.K += "<li data-target=\"#carousel-" + iIndexTemp + "\" data-slide-to=\"" + i + "\" class=\"active\"></li>";
                        objRet.V += "<div class=\"item active\"><img src=\"../web/resource?fid=" + arrTemp[i] + "\" alt=\"" + objInput.sTitle + "\"></div>";
                    }
                    else
                    {
                        objRet.K += "<li data-target=\"#carousel-" + iIndexTemp + "\" data-slide-to=\"" + i + "\"></li>";
                        objRet.V += "<div class=\"item\"><img src=\"../web/resource?fid=" + arrTemp[i] + "\" alt=\"" + objInput.sTitle + "\"></div>";
                    }
                }
                //}
            }
            return objRet;
        }

        //protected String getListNodeImage(Object sTitle, Object sLinkImage)
        //{
        //    String sRet = Convert.ToString(sLinkImage);
        //    if (!String.IsNullOrEmpty(sRet))
        //    {
        //        String[] arrTemp = sRet.Split(';');
        //        sRet = "";
        //        for (int i = 0; i < arrTemp.Length; i++)
        //        {
        //            if (i == 0)
        //                sRet += "<div class=\"item active\"><img src=\"" + arrTemp[i] + "\" alt=\"" + Convert.ToString(sTitle) + "\"></div>";
        //            else
        //                sRet += "<div class=\"item\"><img src=\"" + arrTemp[i] + "\" alt=\"" + Convert.ToString(sTitle) + "\"></div>";
        //        }
        //    }
        //    return sRet;
        //}
    }
}